package com.jsnkj.basic.mvvm

import android.content.Intent
import android.graphics.Point
import android.net.Uri
import android.os.Build
import android.view.MotionEvent
import android.view.View
import androidx.annotation.RequiresApi
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import com.jsnkj.basic.dialog.OpenResultDialog
import com.jsnkj.basic.util.DoubleClickHelper1
import com.jsnkj.basic.util.SharePreferencesUtil
import java.lang.reflect.Field


abstract class BaseKtActivity<VDB : ViewDataBinding, VM : ViewModel> :
    BaseMVVMActivity<VDB, VM>() {

    var screenWidth = 0

    //打开红包弹窗
    var openResultDialog: OpenResultDialog? = null

    override fun onStart() {
        super.onStart()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            showFloatingView(getRootBanding().imgKf)
        }
    }


    @RequiresApi(Build.VERSION_CODES.HONEYCOMB)
    fun showFloatingView(view: View) {
        try {
            view.visibility = View.VISIBLE

            var paramsX = SharePreferencesUtil.getInstance().getInt("kf_base_params_x", 0)
            var paramsY = SharePreferencesUtil.getInstance().getInt("kf_base_params_y", 0)
            if (0 == paramsX) {
                val displaysize = Point()
                windowManager.defaultDisplay.getSize(displaysize)
                paramsX = displaysize.x - 180
                view.x = paramsX.toFloat()
                SharePreferencesUtil.getInstance().putInt("kf_base_params_x", paramsX)
            }

            if (0 == paramsY) {
                paramsY = 300
                SharePreferencesUtil.getInstance().putInt("kf_base_params_y", paramsY)
            }

            view.x = paramsX.toFloat()
            view.y = paramsY.toFloat()

            // 设置悬浮按钮的触摸事件监听
            view.setOnTouchListener(object : View.OnTouchListener {
                private var offsetX = 0f
                private var offsetY = 0f
                private var initialTouchX = 0f
                private var initialTouchY = 0f

                private var rawX = 0f
                private var rawY = 0f

                override fun onTouch(v: View, event: MotionEvent): Boolean {
                    when (event.action) {
                        MotionEvent.ACTION_DOWN -> {
                            rawX = event.rawX
                            rawY = event.rawY

                            offsetX = event.rawX - view.x
                            offsetY = event.rawY - view.y
                            return true
                        }

                        MotionEvent.ACTION_MOVE -> {
                            initialTouchX = event.rawX - offsetX
                            initialTouchY = event.rawY - offsetY
                            view.x = initialTouchX
                            view.y = initialTouchY
                            return true
                        }

                        MotionEvent.ACTION_UP -> {
                            var kf_base_params_x = SharePreferencesUtil.getInstance().getInt("kf_base_params_x", 0)
                            var kf_base_params_y = SharePreferencesUtil.getInstance().getInt("kf_base_params_y", 0)

                            //检测移动的距离，如果很微小可以认为是点击事件
                            if (Math.abs(kf_base_params_x - initialTouchX) < 10 && Math.abs(kf_base_params_y - initialTouchY) < 10) {
                                try {
                                    var field: Field =
                                        View::class.java.getDeclaredField("mListenerInfo")
                                    field.setAccessible(true)
                                    var fileGet: Any = field.get(v)
                                    field = fileGet.javaClass.getDeclaredField("mOnClickListener")
                                    field.setAccessible(true)
                                    fileGet = field.get(fileGet)
                                    if (fileGet != null && fileGet is View.OnClickListener) {
                                        fileGet.onClick(v)
                                    }
                                } catch (e: Exception) {
                                    e.printStackTrace()
                                }
                            }

                            SharePreferencesUtil.getInstance()
                                .putInt("kf_base_params_x", initialTouchX.toInt())
                            SharePreferencesUtil.getInstance()
                                .putInt("kf_base_params_y", initialTouchY.toInt())

                            return true
                        }
                    }
                    return false
                }
            })

            DoubleClickHelper1.click(view) {
                gotoCustomerService()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    fun gotoCustomerService() {
        com.jsnkj.basic.util.LogUtils.e("客服点击事件")
        val content = SharePreferencesUtil.getInstance().getSettingEntity().service_url
        if (content.isNullOrEmpty()) {
            com.jsnkj.basic.util.ToastUtils.show("数据获取失败，请稍后再试")
            return
        }

        try {
            val uri = Uri.parse(content)
            val intent = Intent()
            intent.setAction(Intent.ACTION_VIEW)
            intent.setData(uri)
            //是否安装浏览器
            if (null != intent.resolveActivity(packageManager)) {
                intent.resolveActivity(packageManager)
            } else {
                intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity")
            }
            startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
            com.jsnkj.basic.util.LogUtils.e("跳转异常：" + e.toString())
        }
    }


    fun showResultDialog(money: String) {
        if (null == openResultDialog) {
            openResultDialog = OpenResultDialog(this@BaseKtActivity)
        }
        openResultDialog?.showDialog(money)
    }


    fun closeResultDialog() {
        openResultDialog?.dismiss()
    }


}